<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        var tree = {
            "id": 0,
            "name": "root",
            "left": {
                "id": 1,
                "name": "Simon",
                "left": {
                    "id": 3,
                    "name": "Mandy",
                    "left": {
                        "id": 7,
                        "name": "Lee",
                        "left": {
                            "id": 11,
                            "name": "Fate"
                        }
                    },
                    "right": {
                        "id": 8,
                        "name": "Annie",
                        "left": {
                            "id": 12,
                            "name": "Saber"
                        }
                    }
                },
                "right": {
                    "id": 4,
                    "name": "Tony",
                    "left": {
                        "id": 9,
                        "name": "Candy"
                    }
                }
            },
            "right": {
                "id": 2,
                "name": "right",
                "left": {
                    "id": 5,
                    "name": "Carl",
                },
                "right": {
                    "id": 6,
                    "name": "TOM",
                    "right": {
                        "id": 10,
                        "name": "Kai"
                    }
                }
            }
        }
        // 假设id和name均不会重复，根据输入name找到对应的id
        // 二叉树查找？
        function findIdByName(name) {
            return findIdByNameCall(tree,name);
            function findIdByNameCall(node,key,name='name'){
                if(node[name]===key){
                    return node.id;
                }
                else{
                    return node.left&&findIdByNameCall(node.left,key)||node.right&&findIdByNameCall(node.right,key);
                }
                return null;
            }
        }
        // console.log(findIdByName('TOM'));
        // 假设id和name均不会重复，根据输入id找到对应的name
        function findNameById(id) {
            return findNameByIdCall(tree,id);
            function findNameByIdCall(node,key,id='id'){
                if(node[id]===key){
                    return node.name;
                }
                else{
                    return node.left&&findNameByIdCall(node.left,key)||node.right&&findNameByIdCall(node.right,key);
                }
                return null;
            }
        }
       //  console.log(findNameById(6));
        // 把这个对象中所有的名字以“前序遍历”的方式全部输出到console中
        function getListWithDLR() {
            getListWithDLRCall(tree);
            function getListWithDLRCall(node){
                if(node){
                    console.log(node.name);
                    getListWithDLRCall(node.left);
                    getListWithDLRCall(node.right);
                }
            }
        }
       // getListWithDLR();
        // 把这个对象中所有的名字以“中序遍历”的方式全部输出到console中
        function getListWithLDR() {
            getListWithLDRCall(tree);
            function getListWithLDRCall(node){
                if(node){
                    getListWithLDRCall(node.left);
                    console.log(node.name);                    
                    getListWithLDRCall(node.right);
                }
            }
        }
       // getListWithLDR();
        // 把这个对象中所有的名字以“后序遍历”的方式全部输出到console中
        function getListWithLRD() {
            getListWithLRD(tree);
            function getListWithLRD(node){
                if(node){
                    getListWithLRD(node.left);
                    getListWithLRD(node.right);
                    console.log(node.name);                                        
                }
            }
        }
        // getListWithLRD()
    </script>
</body>

</html>